<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>员工列表</title>
    <script type="text/javascript">
      var newWorkerWin;
      var editWorkerWin;
      Ext.onReady(function(){
        //账号管理---------------------------------------------------------
        //生成有关账号的Grid
        var workerDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/worker/getAllWorkers'
          }),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'worker'
          },[
            {name:'id'},
            {name:'name'},
            {name:'number'},
            {name:'is_locked'},
            {name:'teamName',mapping:'team',
              convert:function(value){
                if(value){
                  return value.name
                }else{
                  return '未设置'
                }
              }}
          ]
        )
        });

        var workerSM = new Ext.grid.CheckboxSelectionModel();
        var workerCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          workerSM,
          {
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderIsLucked,
            width: 70
          },
          {header:'姓名',sortable:true,dataIndex:'name'},
          {header:'指纹编号',sortable:true,dataIndex:'number'},
          {header:'组队',sortable:true,dataIndex:'teamName',width:190}
        ]);

        var workerGrid =  new Ext.grid.GridPanel({
          ds: workerDS,
          cm: workerCM,
          sm: workerSM,
          loadMask: true,
          width: screenWidth-240,
          height: screenHeight-330,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: workerDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '添加员工信息',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newWorkerWin = new Ext.Window({
                  layout: 'fit',
                  width:417,
                  height:245,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  title: '添加员工信息',
                  autoLoad: {
                    url: "/worker/newWorker",
                    scripts: true
                  }
                });
                newWorkerWin.on("destroy",function(){
                  workerDS.reload();
                });
                newWorkerWin.resizable = false;
                newWorkerWin.show();
              }
            },{
              text: '编辑员工信息',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = workerGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  var workerId = records[0].get('id');
                  editWorkerWin = new Ext.Window({
                    title: '编辑员工信息',
                    layout:'fit',
                    width:417,
                    height:250,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/worker/editWorker?workerId=" + workerId,
                      scripts: true
                    }
                  });
                  editWorkerWin.on("destroy",function(){
                    workerDS.reload();
                  });
                  editWorkerWin.resizable = false;
                  editWorkerWin.show();
                }
                
              }
            },
            {
              text:'锁定或锁定员工信息',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(workerGrid.getSelectionModel().hasSelection()){
                  var rs=workerGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var name='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    name+=data.name+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);
                  Ext.Ajax.request({
                    url:'/worker/doLock',
                    method:'post',
                    waitMsg: '正在锁定或锁定员工...',
                    success:function(response,opts){
                      var data = Ext.util.JSON.decode(response.responseText);
                      if(data.success&&data.info=='success') {
                        workerDS.reload();
                      } else {
                        Ext.Msg.alert('提示信息', data.info);
                      }
                    },
                    params: {"id":keys}
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要锁定或锁定的员工');
                }
              }
            },{
              text: '下载员工信息',
              iconCls: 'disk',
              handler: function(){
                var path='/worker/printWorker'
                window.open(path,'_blank','width=1,height=1,toolbar=no,menubar=no,location=no');
              }
            }]
        });


        var workerCBBDS = new Ext.data.Store({
          proxy:new Ext.data.HttpProxy({url:'/worker/getAllWorkers'}),
          reader:new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'worker'
          }, [
            {name:'name'},                            //用作后面的displayField:
            {name:'id'}                                //用作后面的valueField:
          ])
        });
        
        var workername = new Ext.form.ComboBox({  //配合上面的departmentDS，完成动态生成的下拉框
          store: workerCBBDS,
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'worker_id',              //store里面的值，用于显示在表面上的值
          typeAhead: true,
          allowBlank: true,
          minChars:1,
          anchor:'95%',
          mode: 'remote',
          fieldLabel:'姓名',
          forceSelection: true,
          triggerAction: 'all',
          emptyText:'请选择人员.....',
          selectOnFocus:true
        });
        
        var number = new Ext.form.NumberField({
          fieldLabel: '编号',
          anchor: '95%'
        })

        var searchButton = new Ext.Button({
          text:'搜索',
          iconCls: 'searchItem',
          handler: function(){
            workerDS.baseParams.worker_id = workername.getValue();
            workerDS.baseParams.number = number.getValue();
            workerDS.load({
              params:{
                start : 0,
                limit : pageSize
              }
            })
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:50,
          handler: function(){
            searchWorkerForm.form.reset();
          }
        });
        
        var searchWorkerForm = new Ext.FormPanel({
          labelWidth: 75, // label settings here cascade unless overridden
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'right',
          bodyStyle:'padding:5px 5px 0',
          width: screenWidth-240,
          items: [
            {
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .2,
                  layout: 'form',
                  border: false,
                  items: [workername]
                },{
                  columnWidth: .2,
                  layout: 'form',
                  border: false,
                  items: [number]
                },{
                  columnWidth: .18,
                  layout: 'form',
                  border: false,
                  items: [searchButton]
                },{
                  columnWidth: .2,
                  layout: 'form',
                  border: false,
                  items: [clearButton]
                }]
            }]
        });

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-180,
          frame:true,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[searchWorkerForm,workerGrid]
        });
        workbenchPanel.render('workerList');
        workerDS.load();
      })
    </script>
  </head>
  <body>
    <div id="workerList"></div>
  </body>
</html>
